<?php

abstract class MY_Controller extends CI_Controller {
    protected $_nav;

    const CONTENT_REGION = 'content';
    const NAV_REGION = 'nav';
    const TITLE_REGION = 'title';

    const SIDEBAR_NAV_TEMPLATE = 'templates/sidebar_nav';

    public function __construct() {
        parent::__construct();
        $this->_load_nav();
    }

    abstract protected function _get_nav();

    protected function _load_nav() {
        $this->_nav = array('nav' => $this->_get_nav());
    }

    protected function _render() {
        $args = func_get_args();
        $n = func_num_args() - 1;

        if ($n < 2) {
            return;
        }

        $this->template->write(self::TITLE_REGION, $args[0]);

        for ($i = 1; $i < $n; $i++) {
            $this->template->write_view(self::CONTENT_REGION, $args[$i], $args[$n]);
        }

        $this->template->write_view(self::NAV_REGION, self::SIDEBAR_NAV_TEMPLATE, $this->_nav);
        $this->template->render();
    }

    protected function _set_active_view($active) {
        if ($active !== null && isset($this->_nav['nav'][$active])) {
            $this->_nav['nav'][$active]['class'] .= ' active';
        }
    }
}
?>
